


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: MethodInvocationStringGenerator</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.identifier</a> ]
</div>

<h1>Coverage Summary for Class: MethodInvocationStringGenerator (at.schauer.gregor.vienna.identifier)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">MethodInvocationStringGenerator</td>
<td class="coverageStat">
  <span class="percent">
    85.7%
  </span>
  <span class="absValue">
    (6/ 7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    95%
  </span>
  <span class="absValue">
    (19/ 20)
  </span>
</td>
</tr>
  <tr>
    <td class="name">MethodInvocationStringGenerator$SimpleMethodInvocation</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (8/ 8)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (14/ 14)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>total</strong></td>
<td class="coverageStat">
  <span class="percent">
    93.3%
  </span>
  <span class="absValue">
    (14/ 15)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    97.1%
  </span>
  <span class="absValue">
    (33/ 34)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.identifier;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.exception.IdentifierGenerationException;
<i>19</i>&nbsp;import org.aopalliance.intercept.MethodInvocation;
<i>20</i>&nbsp;import org.apache.commons.beanutils.MethodUtils;
<i>21</i>&nbsp;import org.apache.commons.collections.CollectionUtils;
<i>22</i>&nbsp;import org.apache.commons.lang3.ClassUtils;
<i>23</i>&nbsp;import org.springframework.util.Assert;
<i>24</i>&nbsp;import org.springframework.util.ReflectionUtils;
<i>25</i>&nbsp;
<i>26</i>&nbsp;import java.lang.reflect.AccessibleObject;
<i>27</i>&nbsp;import java.lang.reflect.InvocationTargetException;
<i>28</i>&nbsp;import java.lang.reflect.Method;
<i>29</i>&nbsp;import java.util.ArrayList;
<i>30</i>&nbsp;import java.util.List;
<i>31</i>&nbsp;
<i>32</i>&nbsp;/**
<i>33</i>&nbsp; * @author Gregor Schauer
<i>34</i>&nbsp; */
<b class="nc"><i>35</i>&nbsp;public class MethodInvocationStringGenerator&lt;O&gt; implements IdentifierGenerator&lt;String, O&gt; {</b>
<b class="fc"><i>36</i>&nbsp;	protected String separator = &quot; &quot;;</b>
<i>37</i>&nbsp;	protected List&lt;MethodInvocation&gt; invocations;
<i>38</i>&nbsp;
<b class="fc"><i>39</i>&nbsp;	public MethodInvocationStringGenerator(MethodInvocation... invocations) {</b>
<b class="fc"><i>40</i>&nbsp;		addInvocations(invocations);</b>
<b class="fc"><i>41</i>&nbsp;	}</b>
<i>42</i>&nbsp;
<i>43</i>&nbsp;	public MethodInvocationStringGenerator(String separator, MethodInvocation... invocations) {
<b class="fc"><i>44</i>&nbsp;		this(invocations);</b>
<b class="fc"><i>45</i>&nbsp;		this.separator = separator;</b>
<b class="fc"><i>46</i>&nbsp;	}</b>
<i>47</i>&nbsp;
<i>48</i>&nbsp;	public void addInvocations(MethodInvocation... methodInvocations) {
<b class="fc"><i>49</i>&nbsp;		CollectionUtils.addAll(getInvocations(), methodInvocations);</b>
<b class="fc"><i>50</i>&nbsp;	}</b>
<i>51</i>&nbsp;
<i>52</i>&nbsp;	@Override
<i>53</i>&nbsp;	public String generate(O obj) {
<i>54</i>&nbsp;		try {
<b class="fc"><i>55</i>&nbsp;			StringBuilder builder = new StringBuilder();</b>
<b class="fc"><i>56</i>&nbsp;			for (MethodInvocation invocation : invocations) {</b>
<b class="fc"><i>57</i>&nbsp;				builder.append(separator).append(getValue(obj, invocation));</b>
<i>58</i>&nbsp;			}
<b class="fc"><i>59</i>&nbsp;			return builder.length() &gt; 0 ? builder.substring(1) : builder.toString();</b>
<b class="fc"><i>60</i>&nbsp;		} catch (Exception e) {</b>
<b class="fc"><i>61</i>&nbsp;			throw new IdentifierGenerationException(e);</b>
<i>62</i>&nbsp;		}
<i>63</i>&nbsp;	}
<i>64</i>&nbsp;
<i>65</i>&nbsp;	protected Object getValue(O obj, MethodInvocation invocation) throws InvocationTargetException, IllegalAccessException {
<b class="fc"><i>66</i>&nbsp;		Method method = invocation.getMethod();</b>
<b class="fc"><i>67</i>&nbsp;		method.setAccessible(true);</b>
<b class="fc"><i>68</i>&nbsp;		return method.invoke(obj, invocation.getArguments());</b>
<i>69</i>&nbsp;	}
<i>70</i>&nbsp;
<i>71</i>&nbsp;	public List&lt;MethodInvocation&gt; getInvocations() {
<b class="fc"><i>72</i>&nbsp;		return invocations == null ? invocations = new ArrayList&lt;MethodInvocation&gt;() : invocations;</b>
<i>73</i>&nbsp;	}
<i>74</i>&nbsp;
<i>75</i>&nbsp;	public static class SimpleMethodInvocation implements MethodInvocation {
<i>76</i>&nbsp;		Method method;
<i>77</i>&nbsp;		Object[] args;
<i>78</i>&nbsp;		Object target;
<i>79</i>&nbsp;
<i>80</i>&nbsp;		public SimpleMethodInvocation(Class&lt;?&gt; clazz, String methodName, Object... args) {
<b class="fc"><i>81</i>&nbsp;			this(ReflectionUtils.findMethod(clazz, methodName, ClassUtils.toClass(args)), args);</b>
<b class="fc"><i>82</i>&nbsp;		}</b>
<i>83</i>&nbsp;
<b class="fc"><i>84</i>&nbsp;		public SimpleMethodInvocation(Method method, Object... args) {</b>
<b class="fc"><i>85</i>&nbsp;			this.method = method;</b>
<b class="fc"><i>86</i>&nbsp;			this.args = args;</b>
<b class="fc"><i>87</i>&nbsp;		}</b>
<i>88</i>&nbsp;
<i>89</i>&nbsp;		@Override
<i>90</i>&nbsp;		public Method getMethod() {
<b class="fc"><i>91</i>&nbsp;			return method;</b>
<i>92</i>&nbsp;		}
<i>93</i>&nbsp;
<i>94</i>&nbsp;		@Override
<i>95</i>&nbsp;		public Object[] getArguments() {
<b class="fc"><i>96</i>&nbsp;			return args == null ? args = new Object[0] : args;</b>
<i>97</i>&nbsp;		}
<i>98</i>&nbsp;
<i>99</i>&nbsp;		@Override
<i>100</i>&nbsp;		public Object proceed() throws Throwable {
<b class="fc"><i>101</i>&nbsp;			Assert.notNull(target, &quot;Target instance must not ne null.&quot;);</b>
<b class="fc"><i>102</i>&nbsp;			return MethodUtils.invokeMethod(target, method.getName(), args);</b>
<i>103</i>&nbsp;		}
<i>104</i>&nbsp;
<i>105</i>&nbsp;		@Override
<i>106</i>&nbsp;		public Object getThis() {
<b class="fc"><i>107</i>&nbsp;			return target;</b>
<i>108</i>&nbsp;		}
<i>109</i>&nbsp;
<i>110</i>&nbsp;		public void setThis(Object target) {
<b class="fc"><i>111</i>&nbsp;			this.target = target;</b>
<b class="fc"><i>112</i>&nbsp;		}</b>
<i>113</i>&nbsp;
<i>114</i>&nbsp;		@Override
<i>115</i>&nbsp;		public AccessibleObject getStaticPart() {
<b class="fc"><i>116</i>&nbsp;			return method;</b>
<i>117</i>&nbsp;		}
<i>118</i>&nbsp;	}
<i>119</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:23</div>
</div>
</body>
</html>
